package com.wonder.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.PrdIronTray;
import com.wonder.app.mapper.PrdIronTrayDao;
import com.wonder.app.request.PrdIronTrayRequest;
import com.wonder.app.service.PrdIronTrayService;
import com.wonder.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.Map;

@Service
public class PrdIronTrayServiceImpl extends ServiceImpl<PrdIronTrayDao, PrdIronTray> implements PrdIronTrayService {
    /** 日志 */
    private static final Logger _logger = LoggerFactory.getLogger(PrdIronTrayServiceImpl.class);

    @Autowired
    private PrdIronTrayDao prdIronTrayDao;

    /**
     * 校验铁托是否存在
     * @param map
     * @return
     */
    @Override
    public boolean verifyIronTray(Map<String, Object> map) throws Exception {
        String billNo = VtuzxUtil.getString(map,"billNo");
        PrdIronTray prdIronTray = prdIronTrayDao.selectOne(new LambdaQueryWrapper<PrdIronTray>()
                .eq(PrdIronTray::getBillNo, billNo));
        if (VtuzxUtil.isEmpty(prdIronTray)){
            throw new RuntimeException("铁托信息不存在,请确认");
        }
        Boolean status = prdIronTray.getStatus();
        if(status){
            throw new RuntimeException("当前铁托已被占用");
        }

        return true;
    }

    @Override
    public IPage<PrdIronTray> selectList(PrdIronTrayRequest prdIronTrayRequest, Page<PrdIronTray> page) {
        LambdaQueryWrapper<PrdIronTray> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(prdIronTrayRequest.getBillNo())) {
            lambdaQueryWrapper.like(PrdIronTray::getBillNo, prdIronTrayRequest.getBillNo());
        }
        lambdaQueryWrapper.orderByDesc(PrdIronTray::getId);
        Page<PrdIronTray> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return selectPage;
    }

    /**
     * 释放铁托
     * @param userName
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    public boolean releaseIron(String userName, Map<String, Object> map) throws Exception {
        Long id = VtuzxUtil.getLong(map,"id");
        _logger.info(String.format("%s释放了铁托%s",userName,id));
        boolean update = this.update(null, Wrappers.<PrdIronTray>lambdaUpdate()
                .eq(PrdIronTray::getId, id)
                .set(PrdIronTray::getStatus, false));
        return update;
    }

    /**
     * 增加铁托
     * @param userName
     * @param map
     * @return
     */
    @Override
    public boolean addIronTray(String userName, Map<String, Object> map) throws Exception {
        String billNo = VtuzxUtil.getString(map,"billNo");
        PrdIronTray prdIronTray1 = prdIronTrayDao.selectOne(new LambdaQueryWrapper<PrdIronTray>()
                .eq(PrdIronTray::getBillNo, billNo));
        if (!VtuzxUtil.isEmpty(prdIronTray1)){
            throw new RuntimeException(String.format("编号为%s的铁托已经存在",billNo));
        }
        PrdIronTray prdIronTray = new PrdIronTray();
        prdIronTray.setBillNo(billNo);
        prdIronTray.setCreateTime(new Date());
        prdIronTray.setStatus(false);
        prdIronTray.setCreateUserName(userName);
        boolean save = this.save(prdIronTray);
        return save;
    }
}
